<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Schedule;

class ScheduleController extends Controller
{
    //
    public function getSchedule(Request $request){
        $schedule = Schedule::orderBy('id')-> get();
        return response([
            'data' => $schedule,
            'code' => 200
        ]);
    }

        public function addSchedule(Request $request){
        $data = $request->data;
        foreach($data['add']??[] as $key => $item){
            Schedule::create([
                'name' => $item['name'],
                'port' => $item['port'],
                'schedule' => $item['schedule'],
                'rate' => $item['rate'],
            ]);
        }
        Schedule::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            Schedule::find($item['id'])->update([
                'name' => $item['name'],
                'port' => $item['port'],
                'schedule' => $item['schedule'],
                'rate' => $item['rate'],
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
